# import sys
# import threading
# import time
# import rclpy
# from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget
# from PyQt5.QtCore import QObject, pyqtSignal, QTimer
# from sensor_msgs.msg import Image       # 图像消息类型
# from cv_bridge import CvBridge          # ROS与OpenCV图像转换类
# from rclpy.node import Node
# from ui.cameraui import *
# import cv2


# class RclCommNode(Node, QMainWindow, Ui_MainWindow):
#     def __init__(self):
#         Node.__init__(self, "ros2_pyqt5_app")  # 初始化ROS节点
#         QMainWindow.__init__(self)  # 初始化QMainWindow
#         self.camera_sub = self.create_subscription(Image, 'image_raw', self.camera_callback, 10)
#         self.setupUi(self)  # 设置UI

#     def camera_callback(self, img):
#         self.show_cv_img(img)  # 显示图片

#     def show_cv_img(self, img):
#         img = CvBridge.imgmsg_to_cv2(self, img, 'bgr8')  # 将ROS的图像消息转化成OpenCV图像
#         shrink = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # 转换颜色格式
#         QtImg = QtGui.QImage(shrink.data,
#                              shrink.shape[1],
#                              shrink.shape[0],
#                              shrink.shape[1] * 3,
#                              QtGui.QImage.Format_RGB888)
#         jpg_out = QtGui.QPixmap(QtImg).scaled(
#             self.label.width(), self.label.height())  # 调整大小
#         self.label.setPixmap(jpg_out)  # 显示图像



# def main(args=None):  # ROS2节点主入口main函数
#     # 在创建任何GUI组件之前，首先创建 QApplication 实例
#     app = QApplication(sys.argv)  # 创建 QApplication 实例

#     rclpy.init(args=args)  # ROS2 Python接口初始化
#     node = RclCommNode()  # 创建ROS2节点对象并进行初始化
#     node.show()  # 显示主窗口

#     rclpy.spin(node)  # 循环等待ROS2退出
#     node.destroy_node()  # 销毁节点对象
#     rclpy.shutdown()

#     sys.exit(app.exec_())  # 启动应用的事件循环


# if __name__ == '__main__':
#     main()



# import sys
# import threading
# import time
# import rclpy
# from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget
# from PyQt5.QtCore import QObject, pyqtSignal, QTimer
# from sensor_msgs.msg import Image       # 图像消息类型
# from cv_bridge import CvBridge          # ROS与OpenCV图像转换类
# from rclpy.node import Node
# from ui.cameraui import *
# import cv2
# import numpy as np


# class RclCommNode(Node, QMainWindow, Ui_MainWindow):
#     def __init__(self):
#         Node.__init__(self, "ros2_pyqt5_app")  # 初始化ROS节点
#         QMainWindow.__init__(self)  # 初始化QMainWindow
#         self.camera_sub = self.create_subscription(Image, '/image_raw', self.camera_callback, 10)
#         self.setupUi(self)  # 设置UI
#         self.pushButton.clicked.connect(self.onPushbutton)
#         self.state = 1
#         self.pushButton.setText('STOP')
#         self.imgzero = cv2.imread('/home/crt/ros2_ws/src/ros2_pyqt5_app/ros2_pyqt5_app/nailong.jpg')
#         shrink = cv2.cvtColor(self.imgzero, cv2.COLOR_BGR2RGB)  # 转换颜色格式
#         QtImg = QtGui.QImage(shrink.data,
#                             shrink.shape[1],
#                             shrink.shape[0],
#                             shrink.shape[1] * 3,
#                             QtGui.QImage.Format_RGB888)
#         jpg_out = QtGui.QPixmap(QtImg).scaled(
#             self.label.width(), self.label.height())  # 调整大小
#         self.label.setPixmap(jpg_out)  # 显示图像
#         self.setFixedSize(self.width(), self.height())

#     def camera_callback(self, img):
#         if self.state:
#             self.show_cv_img(img)  # 显示图片

#     def show_cv_img(self, img):
#         bridge = CvBridge()  # 创建一个CvBridge对象
#         img = bridge.imgmsg_to_cv2(img, 'bgr8')  # 将ROS的图像消息转化成OpenCV图像
#         shrink = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # 转换颜色格式
#         QtImg = QtGui.QImage(shrink.data,
#                              shrink.shape[1],
#                              shrink.shape[0],
#                              shrink.shape[1] * 3,
#                              QtGui.QImage.Format_RGB888)
#         jpg_out = QtGui.QPixmap(QtImg).scaled(
#             self.label.width(), self.label.height())  # 调整大小
#         self.label.setPixmap(jpg_out)  # 显示图像
    
#     def onPushbutton(self):
#         self.state = 1- self.state
#         if self.state:
#             self.pushButton.setText('STOP')
            
           
#         else:
#             self.pushButton.setText('PLAY')
#             # imgzero = np.zeros((600,600, 3),dtype=np.uint8)
#             shrink = cv2.cvtColor(self.imgzero, cv2.COLOR_BGR2RGB)  # 转换颜色格式
#             QtImg = QtGui.QImage(shrink.data,
#                                 shrink.shape[1],
#                                 shrink.shape[0],
#                                 shrink.shape[1] * 3,
#                                 QtGui.QImage.Format_RGB888)
#             jpg_out = QtGui.QPixmap(QtImg).scaled(
#                 self.label.width(), self.label.height())  # 调整大小
#             self.label.setPixmap(jpg_out)  # 显示图像



# # 创建一个独立的线程来运行 ROS2 节点
# def spin_thread(node):
#     rclpy.spin(node)


# def main(args=None):  # ROS2节点主入口main函数
#     # 在创建任何GUI组件之前，首先创建 QApplication 实例
#     app = QApplication(sys.argv)  # 创建 QApplication 实例

#     rclpy.init(args=args)  # ROS2 Python接口初始化
#     node = RclCommNode()  # 创建ROS2节点对象并进行初始化
#     node.show()  # 显示主窗口

#     # 启动一个新线程运行 ROS2 spin
#     threading.Thread(target=spin_thread, args=(node,), daemon=True).start()

#     sys.exit(app.exec_())  # 启动应用的事件循环


# if __name__ == '__main__':
#     main()




import sys
import threading
import time
import rclpy
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget
from PyQt5.QtCore import QObject, pyqtSignal, QTimer
from sensor_msgs.msg import Image 
from cv_bridge import CvBridge
from rclpy.node import Node
from ui.show_ui import *
import cv2
import numpy as np
from yolo_msg.msg import Yolo


class RclCommNode(Node, QMainWindow, Ui_MainWindow):
    def __init__(self):
        Node.__init__(self, "show")
        QMainWindow.__init__(self)
        self.setupUi(self)  # 设置UI
        self.camera_sub0 = self.create_subscription(Image, '/img0', self.camera0_callback, 10)
        self.camera_sub1 = self.create_subscription(Image, '/img1', self.camera1_callback, 10)
        self.yolo_sub = self.create_subscription(Yolo, '/face', self.face_callback, 10)
        #self.control_pub = self.create_publisher()
        self.Button_help0.setCheckable(False)
        self.Button_help1.setCheckable(False)
        self.Button_ignore.setCheckable(False)

    def camera0_callback(self, img):
        self.show_cv_img(img, 0)
    def camera1_callback(self, img):
        self.show_cv_img(img, 1)

    def show_cv_img(self, img, id):
        bridge = CvBridge()
        img = bridge.imgmsg_to_cv2(img, 'bgr8')
        shrink = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        QtImg = QtGui.QImage(shrink.data,
                             shrink.shape[1],
                             shrink.shape[0],
                             shrink.shape[1] * 3,
                             QtGui.QImage.Format_RGB888)
        if id == 0:
            jpg_out = QtGui.QPixmap(QtImg).scaled(
                self.imgshow0.width(), self.imgshow0.height()) 
            self.imgshow0.setPixmap(jpg_out)
        elif id == 1:
            jpg_out = QtGui.QPixmap(QtImg).scaled(
                self.imgshow1.width(), self.imgshow1.height()) 
            self.imgshow1.setPixmap(jpg_out)
    def face_callback(self, msg):
        pass
        
def spin_thread(node):
    rclpy.spin(node)

def main(args=None):
    app = QApplication(sys.argv) 

    rclpy.init(args=args)
    node = RclCommNode() 
    node.show()
    threading.Thread(target=spin_thread, args=(node,), daemon=True).start()

    sys.exit(app.exec_())